using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using BL.Client.BudgetManageBL;

namespace UI.BudgetManage.UIFunction
{
    class DetailDataGridViewBound
    {
        private DataGridView dgvMain;
        private DataGridView dgvDetails;
        private Panel plDetails;
        private LinkLabel llDetails;
        private DataGridViewLinkColumn dgvlcMain;

        private int ColumnIndex;
        private int RowIndex;
        public delegate DataSet ShowDetails(string id);
        public event ShowDetails OnShowDetails;

        public DetailDataGridViewBound(DataGridView dgvmain, DataGridView dgvdetails,
                                        Panel pldetails, LinkLabel lldetails)
        {
            this.dgvMain = dgvmain;
            this.dgvDetails = dgvdetails;
            this.plDetails = pldetails;
            this.llDetails = lldetails;
            this.dgvMain.Controls.Add(this.plDetails);

            this.plDetails.Visible = false;
        }


        public DetailDataGridViewBound(DataGridView dgvmain, DataGridView dgvdetails)
        {
            this.dgvMain = dgvmain;
            this.dgvDetails = dgvdetails;
            this.dgvMain.Controls.Add(this.dgvDetails);

            this.plDetails.Visible = false;
        }

        public void BudgetItemShowBound(string headertext, string text, string name)
        {
            this.dgvlcMain = new DataGridViewLinkColumn();
            dgvlcMain.HeaderText = headertext;
            dgvlcMain.UseColumnTextForLinkValue = true;
            dgvlcMain.Text = text;
            dgvlcMain.Name = name;
            this.dgvMain.Columns.Insert(0, this.dgvlcMain);
            this.dgvMain.CellContentClick += new DataGridViewCellEventHandler(DGV_CellContentClick);
            this.dgvMain.DataBindingComplete +=new DataGridViewBindingCompleteEventHandler(dgvMain_DataBindingComplete);
            this.dgvMain.Scroll += new ScrollEventHandler(dgvMain_Scroll);
            this.llDetails.LinkClicked += new LinkLabelLinkClickedEventHandler(llDetails_LinkClicked);
        }

        void dgvMain_Scroll(object sender, ScrollEventArgs e)
        {
            if (this.plDetails.Visible && e.ScrollOrientation == ScrollOrientation.VerticalScroll )
            {
                this.LocationDetails();  
            } 
        }

        private void LocationDetails()
        {
            Rectangle linkcell = this.dgvMain.GetCellDisplayRectangle(this.ColumnIndex, this.RowIndex, false);
            
            if (linkcell.Height == 0)
            {
                plDetails.Visible = false;
            }
            else
            {
                plDetails.Top = linkcell.Bottom;
                if (plDetails.Bottom > this.dgvMain.Bottom)
                    plDetails.Top = linkcell.Top - plDetails.Height;
                plDetails.Left = linkcell.Left;
            }
        }
        private void DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMain.Columns[e.ColumnIndex].Name == dgvlcMain.Name)
            {
                if (plDetails.Visible && e.ColumnIndex == this.ColumnIndex && e.RowIndex == this.RowIndex)
                    plDetails.Visible = false;
                else
                {
                    dgvMain.Rows[e.RowIndex].Selected = true;
                    plDetails.Visible = true;
                    plDetails.BorderStyle = BorderStyle.FixedSingle;
                    string budget_id = this.dgvMain.Rows[e.RowIndex].Cells["BudgetId"].Value.ToString().Trim();
                    if (OnShowDetails != null)
                    {
                        dgvDetails.DataSource = ((DataSet)this.OnShowDetails.Invoke(budget_id)).Tables[0];
                    }

                    this.ColumnIndex = e.ColumnIndex;
                    this.RowIndex = e.RowIndex;
                    this.LocationDetails();
                }
            }
        }

        private void dgvMain_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.dgvDetails.DataSource = null;
            this.plDetails.Visible = false;
        }
        private void llDetails_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            plDetails.Visible = false;
        }
        
    }
}
